package com.we.scrm.web;

import com.we.scrm.common.enums.UserStatusEnum;
import com.we.scrm.common.page.Pagination;
import com.we.scrm.common.shiro.ShiroContext;
import com.we.scrm.common.util.JsonView;
import com.we.scrm.domain.AuthUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class AuthUserController  extends  AbstractController {
    @RequestMapping("/authUserPage")
    public ModelAndView authUserPage() {
        return prepareModelAndView("authUserPage");
    }
    @PostMapping(value = "/authUser/list")
    @ResponseBody
    public String authUserList(AuthUser queryEntity, Pagination<AuthUser> page) {
        page = authUserService.queryPage(queryEntity, page);
        return JsonView.successJson(page);
    }
    @PostMapping(value = "/authUser/statusList")
    @ResponseBody
    public String authUserStatusList() {
        return JsonView.successJson(UserStatusEnum.toList());
    }
    @PostMapping(value = "/authUser/get")
    @ResponseBody
    public String getUser(Long id){
        AuthUser user = authUserService.getById(id);
        return JsonView.successJson(user);
    }
    @PostMapping(value = "/authUser/update")
    @ResponseBody
    public String userUpdate(AuthUser entity){
        entity.setUsername(null);
        entity.setName(null);
        authUserService.update(entity);
        return JsonView.successJson();
    }
    @GetMapping("/login")
    public ModelAndView login(Integer f) {
        return prepareModelAndView("authLogin");
    }
    @PostMapping("/login")
    @ResponseBody
    public String login(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            @RequestParam("identiryCode") String identiryCode,
            HttpServletRequest request, HttpServletResponse response) {

        if(password.length() != 64){
            System.out.println("验证码错误");

            return JsonView.failureJson("密码错误");

        }
        //验证码
        Object sessionCode = ShiroContext.getSession().getAttribute(ShiroContext.IDENTIFY_CODE);
        if(null == sessionCode || !identiryCode.equalsIgnoreCase(sessionCode.toString())){
            System.out.println("验证码错误");

            return JsonView.failureJson("验证码错误");

        }
        //校验密码
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);
        }catch (AuthenticationException e){
            System.out.println("校验密码异常");
            return JsonView.failureJson("校验密码异常");
        }
        return JsonView.successJson();
    }
    @PostMapping("/logout")
    @ResponseBody
    public String logout() {
        ShiroContext.logout();
        return JsonView.successJson();
    }
}
